package com.sxkiler.demo.junitcase;

import com.sxkiler.demo.CloudMonitorApplication;
import com.sxkiler.demo.elasticsearch.entity.Demo;
import com.sxkiler.demo.elasticsearch.entity.RoomVo;
import org.apache.solr.common.SolrInputDocument;
import org.frameworkset.elasticsearch.boot.BBossESStarter;
import org.frameworkset.elasticsearch.client.ClientInterface;
import org.frameworkset.elasticsearch.entity.ESDatas;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.*;
import com.sxkiler.demo.model.*;

@SpringBootTest
public class ElasticsearchTestCase {
    @Autowired
    private BBossESStarter bbossESStarter;

    @Test
    public void testQueryDoc() throws  Exception{
        ClientInterface clientInterface = bbossESStarter.getConfigRestClient("esmapper/solrtest.xml");
        boolean exist = clientInterface.existIndiceType("query-room","query_roomvo");
        if(exist){
            int totalTime = 0;
            for(int i=0;i<1;i++){

                long start = System.currentTimeMillis();
                Map params = new HashMap<String,Object>();
                params.put("columnName","solrTestProject");
                params.put("columnValue","*89*");
                //执行查询，demo为索引表，_search为检索操作action
                ESDatas<RoomVo> esDatas =  //ESDatas包含当前检索的记录集合，最多1000条记录，由dsl中的size属性指定
                        clientInterface.searchList("query-room/_search",//demo为索引表，_search为检索操作action
                                "querySolrTest",//esmapper/demo.xml中定义的dsl语句
                                params,//变量参数
                                RoomVo.class);//返回的文档封装对象类型
                System.out.println(esDatas.getDatas().size());
                for(RoomVo vo:esDatas.getDatas()){
                    System.out.println(vo.getSolrTestProject());
                }
                totalTime += System.currentTimeMillis()-start;
            }
            System.out.println((totalTime)+"ms");
        }
    }
    @Test
    public void testBatchAdd() throws Exception{
        //判断索引类型是否存在，false表示不存在，正常返回true表示存在
        ClientInterface clientInterface = bbossESStarter.getConfigRestClient("esmapper/solrtest.xml");
        boolean exist = clientInterface.existIndiceType("query-room","query_roomvo");

        //如果索引表demo已经存在先删除mapping
        if(exist) {
            String r = clientInterface.dropIndice("query-room");
            System.out.println(r);
        }
        //创建索引表demo
        clientInterface.createIndiceMapping("query-room",//索引表名称
                "createSolrTestIndice");//索引表mapping dsl脚本名称，在esmapper/demo.xml中定义createDemoIndice

        String demoIndice = clientInterface.getIndice("query-room");//获取最新建立的索引表结构
        System.out.println(demoIndice);

        long start = System.currentTimeMillis();
        for(int i=0;i<400;i++){
            long startTime = System.currentTimeMillis();
            List<RoomVo> solrList = new ArrayList();
            for(int j=0;j<10000;j++){
                RoomVo doc = new RoomVo();
                String id = UUID.randomUUID().toString();
                doc.setId(id);
                doc.setSolrTestName(NameGenerator.getName("TestName",i*10000+j));
                doc.setSolrTestContract(NameGenerator.getName("TestContract",i*10000+j));
                doc.setSolrTestCustomer(NameGenerator.getName("TestCustomer",i*10000+j));
                doc.setSolrTestRoom(NameGenerator.getName("TestRoom",i*10000+j));
                doc.setSolrTestProject(NameGenerator.getName("TestProject",i*10000+j));
                doc.setSolrTestPhone(NameGenerator.getName("TestPhone",i*10000+j));
                solrList.add(doc);
            }
            //向固定index demo添加或者修改文档,如果demoId已经存在做修改操作，否则做添加文档操作，返回处理结果
            String response = clientInterface.addDocuments("query-room",//索引表
                    "query_roomvo",//索引类型
                    solrList);
            System.out.println(response);
            long endTime = System.currentTimeMillis();
            System.out.println("批次提交::"+i+"耗费,"+(endTime-startTime));
        }

        System.out.println("总执行耗时:"+(System.currentTimeMillis()-start));
    }
}
